package com.xindacloud.sms.adapter.config;


import com.xindacloud.sms.adapter.utils.UnionResultHelp;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;


@Configuration
@Aspect
@Slf4j
@Order(Ordered.HIGHEST_PRECEDENCE + 10)
public class UnionResultAspect {

    static final String OBJECT_NAME = "java.lang.Object";

    @Around("execution(* com.xindacloud..*Controller.*(..))")
    public Object dataConvert(ProceedingJoinPoint pjp) throws Throwable {

        Signature signature = pjp.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        String returnName = methodSignature.getReturnType().getName();

        Object result;
        result = pjp.proceed();

        if (OBJECT_NAME.equals(returnName)) {
            result = new UnionResultHelp(UnionResultHelp.StatusEnum.Success, result);
        } else {
            log.info("[检测到非Object类型返回值{},建议使用Object返回值类型以统一返回格式.]", returnName);
        }

        return result;
    }

}
